# coding: utf-8

import copy
import hashlib
import json
import time
import requests


CORP_HOST = 'ltapi.1ketong.com:83'
CORP_KEY = '2292848467428753'
CORP_SECRET = '67w7QqnN7549515X3C96vu2U9B8N221h'


def get_timestamp():
    return int(time.time())


def create_sign(**kwargs):
    sign_kwargs = copy.deepcopy(kwargs)
    key_list = sorted(list(sign_kwargs.keys()))
    sign_string = '&'.join(
        '%s=%s' % (key,sign_kwargs[key]) for key in key_list
        if sign_kwargs[key] not in ['', None]
    )
    sign_string += '&corp_secret=' + CORP_SECRET
    md5 = hashlib.md5()
    md5.update(sign_string.encode())
    sign = md5.hexdigest()
    return sign


def main():
    url = "http://124.160.63.235:81/ykt-ucrm-provider/form/1/ivr/upload"
    data = {
        'corp_key': CORP_KEY,
        'ts': get_timestamp(),
        'validdate': '2020-01-01',
        'filedesc': '提示工程商使用抢单号码拨打',
    }
    sign = create_sign(**data)
    data.update({'sign': sign})

    result = {}
    try:
        response = requests.post(
            url,
            data=data,
            files={
                'data': open('./IVR_20180525.wav', 'rb'),
            }
        )

        result = json.loads(response.content.decode('utf-8'))

        print('*' * 50)
        print(result)
        # {'code': '0', 'message': '上传文件成功', 'filename': '33420171011172644.wav'}
        print('*' * 50)
    except Exception as e:
        print(e)


if __name__ == '__main__':
    main()
